package com.wsz.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.eduservice.client.VodClient;
import com.wsz.eduservice.pojo.Video;
import com.wsz.eduservice.service.VideoService;
import com.wsz.eduservice.mapper.VideoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【edu_video(课程视频)】的数据库操作Service实现
* @createDate 2022-09-24 19:05:15
*/
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
    implements VideoService{

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeByChapterId(String chapterId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        queryWrapper.select("video_source_id");
        List<Video> videos = baseMapper.selectList(queryWrapper);

        List<String> list = new ArrayList<>();

        for (int i = 0; i < videos.size(); i++) {
            String videoSourceId = videos.get(i).getVideoSourceId();
            if(StringUtils.isNotBlank(videoSourceId)){
            list.add(videoSourceId);
            }
        }

        if(list.size() > 0)
        vodClient.deleteBatch(list);

        QueryWrapper<Video> queryWrapperd = new QueryWrapper<>();
        queryWrapperd.eq("chapter_id",chapterId);
        int delete = baseMapper.delete(queryWrapperd);
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        List<Video> videos = baseMapper.selectList(queryWrapper);

        List<String> list = new ArrayList<>();

        for (int i = 0; i < videos.size(); i++) {
            String videoSourceId = videos.get(i).getVideoSourceId();
            if(StringUtils.isNotBlank(videoSourceId)){
                list.add(videoSourceId);
            }
        }

        if(list.size() > 0)
            vodClient.deleteBatch(list);

        QueryWrapper<Video> queryWrapperd = new QueryWrapper<>();
        queryWrapperd.eq("course_id",courseId);

        baseMapper.delete(queryWrapperd);
    }
}




